import CryptoJS from 'crypto-js'

const nonce = (length) => {
  let chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
  let maxPos = chars.length
  let str = ''
  for (let i = 0; i < length; i++) {
    str += chars.charAt(Math.floor(Math.random() * maxPos))
  }
  return str
}
const sign = (params, secret) => {
  let ksort = Object.keys(params).sort()
  let arr = []
  for (let i of ksort) {
    arr.push((i + '=' + params[i]))
  }
  arr.push('secret=' + secret)
  return CryptoJS.MD5(arr.join('&')).toString().toUpperCase()
}
const combineSign = (params) => {
  params._t = Date.parse(new Date()) / 1000
  params._n = nonce(32)
  params._s = sign(params, process.env.VUE_APP_KEY)
  return params
}

export default combineSign
